<div class="form">
    <?php
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'auth-item-_form-form',
        //'errorMessageCssClass'=>'error',
        //'enableAjaxValidation' => true,
        'enableClientValidation' => true,
            ));
    ?>

    <table style="width: 100%">


        <tr>
            <td class="label required"><?php echo $form->labelEx($model, 'type'); ?></td>
            <td class="input" >
                <?php
                echo $form->dropDownList($model, 'type', Html::listData(AuthItemType::model()->findAll(), 'id', 'name'), array(
                    'empty' => Yii::t('messages', 'MSG_FIRST_ITEM_TYPE')
                        //'onchange' => 'onTypeChange(this);',
                        )
                );
                ?>
                <?php echo $form->error($model, 'type'); ?>
            </td>
        </tr>

        <tr>
            <td class="label required"><?php echo $form->labelEx($model, 'name'); ?></td>
            <td class="input" >
                <?php echo $form->textField($model, 'name', array('style' => 'width:230px')); ?>
                <?php echo $form->error($model, 'name'); ?>
            </td>
        </tr>

        <tr>
            <td class="label required"><?php echo $form->labelEx($model, 'description'); ?></td>
            <td class="input" >
                <?php echo $form->textArea($model, 'description', array('style' => 'width:230px')); ?>
                <?php echo $form->error($model, 'description'); ?>
            </td>
        </tr>

        <tr>
            <td class="label required"><?php echo $form->labelEx($model, 'bizrule'); ?></td>
            <td class="input" >
                <?php echo $form->textArea($model, 'bizrule', array('style' => 'width:230px')); ?>
                <?php echo $form->error($model, 'bizrule'); ?>
            </td>
        </tr>

        <tr>
            <td class="label required"><?php echo $form->labelEx($model, 'data'); ?></td>
            <td class="input" >
                <?php echo $form->textArea($model, 'data', array('style' => 'width:230px')); ?>
                <?php echo $form->error($model, 'data'); ?>
            </td>
        </tr>

        <tr>
            <td colspan="2" class="cbutton">
                <?php echo Html::buttonConfirm(t('BTN_SAVE'),'statusConfirmed'); ?>
                <?php echo Html::buttonConfirm(t('BTN_CANCEL'),'statusCanceled', 'cancel'); ?>
            </td>
        </tr>
    </table>
    <?php $this->endWidget(); ?>

</div><!-- form -->

<script type="text/javascript">
    
    LGCMS.addTooltip();
    var AUTHITEMTYPES = {
        'OPERATION': "0",
        'TASK': "1",
        'ROLE': "2"
    };
    $(document).ready(function() {
        var nameSelector = "form#auth-item-_form-form input#AuthItem_name";
        var typeSelector = "form#auth-item-_form-form select#AuthItem_type";
        var currtype = $(typeSelector).val();
        var currname = $(nameSelector).val();
        
        var allOperation;
        
        //override to limit 10 result
        $.ui.autocomplete.prototype._renderMenu = function( ul, items ) {
            var self = this;
            $.each( items, function( index, item ) {
                if (index < 10) // here we define how many results to show
                {self._renderItem( ul, item );}
            });
        }
        function getAllOperations(){
            if(allOperation == undefined){
                jQuery.ajax({
                        async: false,
                        cache: true,
                        type:'POST',
                        url:'GetAllOperation',
                        success:function(data){
                            allOperation = $.parseJSON(data);
                        }
                    });
            }
        }
        
        $(nameSelector).live("focus",function(){
            currtype = $(typeSelector).val();
            if(currtype == AUTHITEMTYPES.ROLE){
                $( nameSelector ).autocomplete({
                            source : []
                });
            }
            else{
                getAllOperations();
                $( nameSelector ).autocomplete({
                    source : allOperation
                });
            }
        });
  
        if(currtype == AUTHITEMTYPES.OPERATION){
            getAllOperations();
            if(allOperation.indexOf(currname) == -1){
                $(nameSelector).val("");
            }
        }
        
        $('button[name="statusConfirmed"]').click(function(){
            getAllOperations();
            if(allOperation.indexOf(currname) == -1 && currtype == AUTHITEMTYPES.OPERATION){
                $(nameSelector).val("");
            }
        });
        
        $(nameSelector).live("change",function(){
            currname = $(this).val();
        });
        
        $(typeSelector).live("change",function()
        {
            getAllOperations();
            currname = $(nameSelector).val();
            currtype = $(this).val();
            if(currtype ==  AUTHITEMTYPES.OPERATION){
                if(allOperation.indexOf(currname) == -1){
                    $( nameSelector ).val("");
                }
            }
        });
        $(nameSelector).live("blur",function(){
            getAllOperations();
            if(currtype ==  AUTHITEMTYPES.OPERATION){
                currname = $(this).val();
                if(allOperation.indexOf(currname) == -1){
                    $(this).val("");
                }
            }
        });
    });
</script>
